package com.alibaba.citrus.ots.price.ability.functions.factory.activity.process;

import com.epoch.app.otspricecenter.model.enums.ActivityTypeEnum;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: qiaojiange
 * @create: 2021/11/18
 * @Description 活动项目处理器 管理类
 */
@Component
public class ActivityProcessManager implements ApplicationContextAware {

    private static Map<String, ActivityProcess> map = new ConcurrentHashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        String[] beanNamesForType = applicationContext.getBeanNamesForType(ActivityProcess.class);
        for (String beanName : beanNamesForType) {
            ActivityProcess bean = applicationContext.getBean(beanName, ActivityProcess.class);
            map.put(bean.getActivityTypeDesc(), bean);
            //买赠和搭赠使用同一个活动处理器，如果以后有特殊逻辑可以单独定义并修订
            if (bean.getActivityTypeDesc().equals(ActivityTypeEnum.BUY_GIFT.getValue())) {
                map.put(ActivityTypeEnum.TAKE_GIFT.getValue(), bean);
            }
        }
    }

    /**
     * 获取活动处理器
     *
     * @param activityTypeDesc
     * @return
     */
    public ActivityProcess getActivityProcess(String activityTypeDesc) {
        return map.get(activityTypeDesc);
    }
}
